package prv.pursuivant.config.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import prv.pursuivant.config.Config;
import prv.pursuivant.config.ConfigSerializer;

import com.google.inject.Inject;
import com.thoughtworks.xstream.XStream;

/**
 * Config serializer that uses XStream library to read/write configuration to
 * XML file
 * 
 * @author Kwinto
 * 
 */

public class XStreamConfigSerializer implements ConfigSerializer {

	protected final XStream xstream;

	/**
	 * Constructs factory/serializer
	 * 
	 */
	@Inject
	public XStreamConfigSerializer(XStream xstream) {
		this.xstream = xstream;
	}

	@Override
	public Config readConfig(InputStream stream) throws IOException {
		Object fromXML = xstream.fromXML(stream);
		final Config config = (Config) fromXML;
		return config;
	}

	@Override
	public void writeConfig(Config config, OutputStream stream)
			throws IOException {
		xstream.toXML(config, stream);
	}

}
